/*
 * @Author: dadadaXU 1413107032@qq.com
 * @Date: 2025-02-16 21:27:16
 * @LastEditors: dadadaXU 1413107032@qq.com
 * @LastEditTime: 2025-02-16 22:56:20
 * @FilePath: \LeetCode\121.买卖股票的最佳时机.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @lc app=leetcode.cn id=121 lang=cpp
 *
 * [121] 买卖股票的最佳时机
 *
 * 方法1：双指针
 * - in： 买入时机 prices[out] < prices[in] 更新 in
 * - out：卖出时机 out++
 */

#include <vector>
#include <iostream>

// @lc code=start
class Solution
{
public:
    int maxProfit(std::vector<int> &prices)
    {
        const int len = prices.size();
        if (len == 1)
            return 0;

        int max_interest = 0;
        int in = 0, out = 1;
        do
        {
            max_interest = std::max(max_interest, prices[out] - prices[in]);
            if (prices[out] < prices[in])
                in = out;
            out++;
        } while (out < len);

        return max_interest;
    }
};
// @lc code=end

int main(void)
{
    Solution solution;
    std::vector<int> prices = {1, 0};

    std::cout << solution.maxProfit(prices) << std::endl;

    return 0;
}